package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.dto.ClinicMedicalDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.medical.ClinicMedicalService;
import com.zshan.clinic.admin.service.medical.OrderExamService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.OrderExamExec;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 查验订单
 * @author lichen
 */
@RestController
@RequestMapping("/order/exam")
public class OrderExamController extends AbstractController {


    @Autowired
    private OrderExamService orderExamService;


    /**
     * 患者：查询患者检查/检验记录
     * @param patientId
     * @param projectCategory 1检查2检验 projectCategoryEnum
     * @param visitDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientExamList")
    public RestResp<PageInfo<OrderExamPatientVo>> getPatientExamList(@RequestParam(name = "patientId") Long patientId,
                                                                     @RequestParam(name = "projectCategory") String projectCategory,
                                                                     @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                                     @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<OrderExamPatientVo> examList = orderExamService.getPatientExamList(userInfo.getClinicId(), patientId,projectCategory,visitDate,pageNo,pageSize);
        return RestResp.SUCCESS(examList);
    }


    /**
     * 检查：查询诊所今日待检查数
     * @return
     */
    @GetMapping("/getWaitingCount")
    public RestResp<OrderExamWaitingVo> getWaitingCount(@RequestParam(name = "projectCategory") String projectCategory) {
        UserInfo userInfo = getUserInfo();
        return RestResp.SUCCESS(orderExamService.getWaitingCount(userInfo.getClinicId(),projectCategory));
    }


    /**
     * 检查：查询患者历史的检查单数量
     * @param patientId 患者ID
     * @param projectCategory 项目分类projectCategoryEnum
     * @return
     */
    @GetMapping("/getOrderExamHistoryCount")
    public RestResp<Long> getOrderExamHistoryCount(@RequestParam(name = "patientId") Long patientId,
                                                                      @RequestParam(name = "projectCategory",required = false) String projectCategory,
                                                                      @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                      @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        Long totalCount = orderExamService.getOrderExamHistoryCount(userInfo.getClinicId(), patientId, projectCategory);
        return RestResp.SUCCESS(totalCount);
    }

    /**
     * 检查：查询患者历史的检查单
     * @param patientId 患者ID
     * @param projectCategory 项目分类projectCategoryEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getOrderExamHistoryList")
    public RestResp<List<OrderExamHistoryVo>> getOrderExamHistoryList(@RequestParam(name = "patientId") Long patientId,
                                                                      @RequestParam(name = "projectCategory",required = false) String projectCategory,
                                                                      @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                      @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderExamHistoryVo> list = orderExamService.getOrderExamHistoryList(userInfo.getClinicId(), patientId, projectCategory, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }

    /**
     * 检查：获取检查单详情
     * @param execId 执行ID
     * @return
     */
    @GetMapping("/getOrderExamDetail")
    public RestResp<OrderExamDetailVo> getOrderExamDetail(@RequestParam(name = "execId") Long execId) {
        OrderExamDetailVo detail = orderExamService.getOrderExamDetail(execId);
        return RestResp.SUCCESS(detail);
    }


    /**
     * 检查：查询审核列表
     * @param projectCategory 项目分类projectCategoryEnum
     * @param patientName 患者名
     * @param visitDate 就诊日期 默认当天
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getAuditList")
    public RestResp<List<OrderExamPageVo>> getAuditList(@RequestParam(name = "projectCategory", required = false) String projectCategory,
                                                        @RequestParam(name = "patientName", required = false) String patientName,
                                                        @RequestParam(name = "visitDate", required = false) LocalDate visitDate,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderExamPageVo> list = orderExamService.getAuditList(userInfo.getClinicId(), projectCategory, patientName, visitDate, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }

    /**
     * 检查：查询检验列表
     * @param projectCategory 项目分类projectCategoryEnum
     * @param patientName 患者名
     * @param visitDate 就诊日期 默认当天
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getExamList")
    public RestResp<List<OrderExamPageVo>> getExamList(@RequestParam(name = "projectCategory", required = false) String projectCategory,
                                                       @RequestParam(name = "patientName", required = false) String patientName,
                                                       @RequestParam(name = "visitDate", required = false) LocalDate visitDate,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderExamPageVo> list = orderExamService.getExamList(userInfo.getClinicId(), projectCategory, patientName, visitDate, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }

    /**
     * 检查：提交审核
     * @param orderExamExec
     * @return
     */
    @PostMapping("/submitAudit")
    public RestResp<Void> submitAudit(@RequestBody OrderExamExec orderExamExec) {
        UserInfo userInfo = getUserInfo();
        orderExamExec.setUpdateUserId(userInfo.getEmpId());
        orderExamExec.setUpdateBy(userInfo.getEmpName());
        orderExamExec.setClinicId(userInfo.getClinicId());
        orderExamService.submitAudit(orderExamExec);
        return RestResp.SUCCESS();
    }

    /**
     * 检查：审核检查单
     * @param orderExamExec
     * @return
     */
    @PostMapping("/auditExam")
    public RestResp<Void> auditExam(@RequestBody OrderExamExec orderExamExec) {
        UserInfo userInfo = getUserInfo();
        orderExamExec.setUpdateUserId(userInfo.getEmpId());
        orderExamExec.setUpdateBy(userInfo.getEmpName());
        orderExamExec.setClinicId(userInfo.getClinicId());
        orderExamService.auditExam(orderExamExec);
        return RestResp.SUCCESS();
    }
}
